﻿//编辑对话框
var dlg = new MyAppDialog(600, 480);

//列定义
var cols = [[
        { field: "Id", title: 'Id', width: 10, hidden: true },
        { field: "Menuname_zho", title: '菜单名称', sortable: true, width: 100, align: 'left' },
        //{
        //    field: "Menukind", title: '分类', sortable: true, width: 80, align: 'left', formatter: function (value, rec) {
        //        if (value == "0")
        //            return "<i class='icon-folder-close'></i> " + rec.Menukindstr;
        //        else if (value == "1")
        //            return "<i class='icon-empty'></i><i class='icon-list'></i> " + rec.Menukindstr;
        //        else if (value == "2")
        //            return "<i class='icon-empty'></i><i class='icon-empty'></i><i class='icofont-key'></i> " + rec.Menukindstr;;
        //    }
        //},
        { field: "Menuid", title: '菜单ID', sortable: true, width: 100, align: 'left' },
        { field: "Orderseq", title: '排序', sortable: true, width: 60, align: 'left' },
        //{ field: "Pmenuid", title: '父菜单', sortable: true, width: 100, align: 'center' },
        { field: "Menuurl", title: 'URL', sortable: true, width: 200, align: 'left' },
        { field: "Menuiconmiddle", title: '图标', sortable: true, width: 100, align: 'left' },
        {
            field: "Status", title: '菜单状态', sortable: true, width: 40, align: 'center', formatter: function (value, rec) {
                if (value == "0")
                    return "Normal";
                else if (value == "1")
                    return "Hidden";
                else if (value == "9")
                    return "Locked";
            }
        },
        //{ field: "op", title: '操作', width: 40, align: 'center', formatter: function (value, rec) { return "<a href='#none' onclick='rolemenu(" + rec.Id + ")' class='btn btn-xs btn-primary'>菜单权限</a>" } }
]];

$(document).ready(function () {
    //删除
    $("#btndel").click(function (evt) { evt.preventDefault(); del(); });
    $("#delmenu").click(function (evt) {
        if ($(this).hasClass("menu-item-disabled")) return;

        del();
    });
    //添加
    $("#btnadd").click(function (evt) {
        evt.preventDefault();
        dlg.setURL("新增菜单", baseurl + "Manage/Menu/Edit/0?" + Math.random());
        dlg.show();
    });
    //添加模块
    $("#addfunc").click(function (evt) {
        if ($(this).hasClass("menu-item-disabled")) return;

        dlg.setURL("新增菜单", baseurl + "Manage/Menu/Edit/0?" + Math.random());
        dlg.show();
    });
    //添加同级
    $("#addmenu").click(function (evt) {
        if ($(this).hasClass("menu-item-disabled")) return;

        var selectedRow = $("#datagrid").treegrid("getSelected");

        dlg.setURL("新增菜单",baseurl +  "Manage/Menu/Edit/0?p=" + selectedRow.Pmenuid + "&k=" + selectedRow.Menukind + "&r=" + Math.random());
        dlg.show();
    });
    //添加下级
    $("#addsubmenu").click(function (evt) {
        if ($(this).hasClass("menu-item-disabled")) return;

        var selectedRow = $("#datagrid").treegrid("getSelected");

        dlg.setURL("新增菜单", baseurl + "Manage/Menu/Edit/0?p=" + selectedRow.Menuid + "&k=" + (parseInt(selectedRow.Menukind) + 1) + "&r=" + Math.random());
        dlg.show();
    });
    //查询
    $("#btnquery").click(function (evt) {
        evt.preventDefault();
        var params = $("#datagrid").datagrid('options').queryParams;
        params.s = $("#searchkey").val();
        $("#datagrid").treegrid("load",0);
    });

    $("#searchkey").keydown(function (event) {
        if (event.keyCode == $.ui.keyCode.ENTER) {
            event.preventDefault();
            $("#btnquery").click();
            return false;
        }
    });

    LoadTreegrid();
});

//关闭对话框
function closeDialog(reload) {
    dlg.close();

    if (reload) {
        $("#datagrid").treegrid("reload",0);
    }
}

////加载列表数据
//function LoadDatagrid() {
//    var $dg = $("#datagrid");
//    $dg.datagrid({
//        title: '菜单列表',
//        url: baseurl + 'Manage/Menu/MenuListJson',
//        fit: true,
//        fitColumns: true,
//        striped: true,
//        nowrap: true,
//        idField: 'Id',
//        pagination: true,
//        pageNumber: 1,
//        pageSize: 20,
//        pageList: [10, 20, 30, 45, 60, 75],
//        rownumbers: true,
//        singleSelect: true,
//        loadMsg: "数据加载中...",
//        queryParams: {
//            s: ""
//        },
//        sortName: 'Orderseq',
//        sortOrder: 'asc',
//        columns: cols,
//        onLoadSuccess: function () {
//            //多选时，加载完成后清除所有选中项（不然可能会有残留选中项）
//            $dg.datagrid("clearSelections");
//        },
//        onDblClickRow: function (rowIndex, row) {
//            dlg.setURL("编辑菜单", "/Manage/Menu/Edit/" + row.Id + "?" + Math.random());
//            dlg.show();
//        },
//        onSelect: function (rowIndex, row) {
//            var selectedRows = $dg.datagrid("getSelections");
//            if (selectedRows.length == 0) {
//                $("#btndel").attr("disabled", true);
//            } else {
//                $("#btndel").attr("disabled", false);
//            }
//        },
//        onUnselect: function (rowIndex, row) {
//            var selectedRows = $dg.datagrid("getSelections");
//            if (selectedRows.length == 0) {
//                $("#btndel").attr("disabled", true);
//            } else {
//                $("#btndel").attr("disabled", false);
//            }
//        }
//    });
//    //PaginationInit();
//}


//加载列表数据
function LoadTreegrid() {
    var $dg = $("#datagrid");
    $dg.treegrid({
        //title: 'CRF字段列表',
        url: baseurl + 'Manage/Menu/MenuListTreeJson',
        fit: true,
        fitColumns: true,
        animate: true,
        rownumbers: true,
        striped: true,
        nowrap: true,
        idField: 'Menuid',
        treeField: 'Menuname_zho',
        queryParams: {
            s: ""
        },
        columns: cols,
        singleSelect: true,
        toolbar: "#datagrid_toolbar",
        onLoadSuccess: function (row) {
            ////支持拖拽
            //$(this).treegrid('enableDnd', row ? row.id : null);
        },
        onDblClickRow: function (row) {
            dlg.setURL("编辑菜单", baseurl + "Manage/Menu/Edit/" + row.Id + "?" + Math.random());
            dlg.show();
        },
        onContextMenu: function (e, row) {
            e.preventDefault();

            if (row.Menukind == "2") {
                $('#popmenu').menu("disableItem", $("#addsubmenu"));
            } else {
                $('#popmenu').menu("enableItem", $("#addsubmenu"));
            }

            $dg.treegrid("select", row.Menuid);
            $('#popmenu').menu('show', {
                hideOnUnhover: false,
                left: e.pageX,
                top: e.pageY
            });
        }
    });
    //PaginationInit();
}

//删除
function del() {
    var row = $("#datagrid").datagrid('getSelections');
    if (row.length == 0) {
        $.messager.alert(csms_lang.message_info_title, csms_lang.message_delete_choosedelete);
        return;
    }
    if (confirm(csms_lang.message_delete_confrim)) {
        var ids = "";
        for (var i = 0; i < row.length; i++) {
            ids += row[i].Id + ",";
        }

        $.ajax({
            type: "post",
            url: baseurl + "Manage/Menu/Delete",
            data: { ids: ids },
            success: function (e) {
                if (e.Ret == "0") {
                    LoadTreegrid();
                } else {
                    $.messager.alert(csms_lang.message_error,e.Msg);
                }
            }
        });
    }
}